﻿using System.Drawing;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using Color = Microsoft.Xna.Framework.Color;
using Rectangle = Microsoft.Xna.Framework.Rectangle;

namespace Trippy.Animation
{
    public class AnimatedSprite
    {
        
        public int Height { get; protected set; }
        public int Width { get; protected set; }
        public int VerticalBorder { get; protected set; }
        public int HorizontalBorder { get; protected set; } //borders between seperate sprites in sheet
        public int VerticalTrim { get; protected set; }
        public int HorizontalTrim { get; protected set; }
        public int FrameCount { get; protected set; }
        public float AnimationSpeed { get; protected set; }
        public float CurrentFrame;
        public int CurrentMode { get; protected set; } //mode = which line on the sprite sheet we're cycling through

        public Color Tint { get; set; }

        private Texture2D spriteSheet;
        public static SpriteBatch spriteBatch;
        public DrawableTrippyComponent Component { get; protected set; }

        public void Step()
        {
            CurrentFrame += AnimationSpeed;
            if (Math.Floor(CurrentFrame) >= ((float) FrameCount))
            {
                CurrentFrame = 0.0f;
            }
        }

        public void ChangeAnimation(int line, int numframes = -1)
        {
            CurrentMode = line;
            if (numframes >= 0)
                FrameCount = numframes;
            if (Math.Floor(CurrentFrame) >= ((float) FrameCount))
                CurrentFrame = 0.0f;
        }

        public AnimatedSprite(DrawableTrippyComponent component, Texture2D spriteSheet, int spriteHeight, int spriteWidth, int verticalBorder, int horizontalBorder, int verticalTrim, int horizontalTrim, int numFrames, float animationSpeed)
        {
            Tint = Color.White;
            Component = component;
            CurrentFrame = 0.0f;
            AnimationSpeed = animationSpeed;
            CurrentMode = 0;
            FrameCount = numFrames;
            HorizontalBorder = horizontalBorder;
            VerticalBorder = verticalBorder;
            VerticalTrim = verticalTrim;
            HorizontalTrim = horizontalTrim;

            this.spriteSheet = spriteSheet;

            Height = spriteHeight;
            Width = spriteWidth;
        }

        public void Draw(Vector2 position, Vector2 offset, Vector2 scale)
        {
            float viewWidth = spriteBatch.GraphicsDevice.Viewport.Width/Component.CurrentLevel.Camera.Zoom;
            float viewHeight = spriteBatch.GraphicsDevice.Viewport.Height/Component.CurrentLevel.Camera.Zoom;
            RectangleF viewRegion = new RectangleF(Component.CurrentLevel.Camera.Position.X - viewWidth / 2f, Component.CurrentLevel.Camera.Position.Y - viewHeight /2f, viewWidth, viewHeight);

            float x = position.X - offset.X*scale.X;
            float y = position.Y - offset.Y*scale.Y;
            float width = Width * scale.X;
            float height = Height*scale.Y;
            RectangleF target = new RectangleF(x, y, width, height);
            
            if (viewRegion.Contains(target) || viewRegion.IntersectsWith(target))
            {

                spriteBatch.Begin(SpriteSortMode.BackToFront,
                                  BlendState.AlphaBlend,
                                  null,
                                  null,
                                  null,
                                  null, Component.CurrentLevel.Camera.Transform);


                spriteBatch.Draw(spriteSheet,
                                 new Rectangle((int)x,
                                               (int)y, (int) width,
                                               (int) height),
                                 new Rectangle((Width + HorizontalBorder)*Convert.ToInt32(Math.Floor(CurrentFrame)),
                                               (Height + VerticalBorder)*CurrentMode,
                                               Width, Height),
                                 Tint);
                spriteBatch.End();
            }
        }
    }
}
